package graphic;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;

import javax.xml.namespace.QName;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;


import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;


public class TestXpath {


	public static Object  evaluerSAX(String fichier, String expression, QName retour) throws FileNotFoundException{
		Object resultat=null;
		try{
			//création de la source
			InputSource source = new InputSource(new FileInputStream(new File(fichier)));

			//création du XPath 
			XPathFactory fabrique = XPathFactory.newInstance();
			XPath xpath = fabrique.newXPath();

			//évaluation de l'expression XPath
			XPathExpression exp =  xpath.compile(expression);
			resultat = (Object)exp.evaluate(source,retour);
	//		System.out.println(resultat);
			return resultat;
		}catch(XPathExpressionException xpee){
			xpee.printStackTrace();
		}
		return resultat;
	}
	
//	public static Element rid(String inPath,String id) throws FileNotFoundException{
////		File in=new File(inPath);
//		Element resultat=null;
//		String exp="//node()[@id='"+id+"']";
//		System.out.println(exp);
//		InputSource source = new InputSource(new FileInputStream(new File(inPath)));
//		resultat=TestXpath.evaluerSAX(source, exp, XPathConstants.NODE);
//		return resultat;
//	}
//	
//	public static Element rfam(String inPath,String fam) throws FileNotFoundException{
//		Element resultat=null;
//		String exp ="//"+fam;
//		System.out.println(exp);
//		InputSource source = new InputSource(new FileInputStream(new File(inPath)));
//		resultat=TestXpath.evaluerSAX(source, exp, XPathConstants.NODE);
//		return resultat;
//		
//		
//	}

	public static void main(String[] args) throws FileNotFoundException{
		
//		Element e= TestXpath.rid("/home/ubuntu/workspace_indigo/ProjetLongJSDAI/src/expressToAltaRica/t.xml", "e26");
//		Element fam=TestXpath.rfam("/home/ubuntu/workspace_indigo/ProjetLongJSDAI/src/expressToAltaRica/t.xml", "Dataflows");
//		NodeList n = fam.getChildNodes();
//		Element ne=(Element)n.item(1);
//		System.out.println(ne);
//		File in=new File(args[0]);
//		String exp="//node()[@id='e26']";
//		TestXpath.evaluerSAX(in, exp, XPathConstants.NODE);
		
		

	} 







}
